/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package br.jm.financeiro.filter;

import java.io.IOException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author joao
 */
public class UtilFaces {

    public UtilFaces() {
    }

    public FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }

    public ExternalContext getExternalContext() {
        return getFacesContext().getExternalContext();
    }

    public HttpServletRequest getRequest() {
        return (HttpServletRequest) getExternalContext().getRequest();
    }

    public HttpServletResponse getResponse() {
        return (HttpServletResponse) getExternalContext().getResponse();
    }

    public HttpSession getHttpSession() {
        return (HttpSession) getFacesContext().getExternalContext().getSession(false);
    }

    public ServletContext getServletContext() {
        return (ServletContext) getExternalContext().getContext();
    }

    public void msg(String destination, String msg) {
        FacesMessage message = new FacesMessage(msg);
        getFacesContext().addMessage(destination, message);
    }

    public static void addErrorMessage(Exception ex, String defaultMsg) {
        String msg = ex.getLocalizedMessage();
        if (msg != null && msg.length() > 0) {
            addErrorMessage(msg);
        } else {
            addErrorMessage(defaultMsg);
        }
    }

    public static void addErrorMessages(List<String> messages) {
        for (String message : messages) {
            addErrorMessage(message);
        }
    }

    public static void addErrorMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
        FacesContext.getCurrentInstance().addMessage(null, facesMsg);
    }

    public static void addSuccessMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
        FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
    }

    public static void addWarnMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN, msg, msg);
        FacesContext.getCurrentInstance().addMessage("warnInfo", facesMsg);
    }

    public static void addFatalMessage(String msg) {
        FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, msg, msg);
        FacesContext.getCurrentInstance().addMessage("fatalInfo", facesMsg);
    }

    public void launchPdfShow(byte[] bytes, boolean isDownload, String fileName) {
        HttpServletResponse response = new UtilFaces().getResponse();
        if (bytes != null && bytes.length > 0) {
            ServletOutputStream ouputStream = null;
            try {
                response.setContentType("application/pdf");

                if (isDownload) {
                    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".pdf");
                }

                response.setContentLength(bytes.length);
                ouputStream = response.getOutputStream();
                ouputStream.write(bytes, 0, bytes.length);
                ouputStream.flush();
                ouputStream.close();
                getFacesContext().responseComplete();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
